import { Entity, PrimaryColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';

// 用户状态枚举
export enum UserStatus {
  NORMAL = 0,      // 正常
  DISABLED = 1,    // 禁用
}

// 性别枚举
export enum Gender {
  FEMALE = 0,      // 女
  MALE = 1         // 男
}

// 缴费状态枚举
export enum PaymentStatus {
  UNPAID = 0,      // 未缴费
  PAID = 1,        // 已缴费
  PARTIAL = 2      // 部分缴费
}

// 消息订阅状态
export enum SubscriptionStatus {
  UNSUBSCRIBED = 0, // 未订阅
  SUBSCRIBED = 1    // 已订阅
}

// 系统用户状态
export enum SystemUserStatus {
  NO = 0,          // 非系统用户
  YES = 1          // 系统用户
}

// 录取结果枚举
export enum AdmissionResult {
  PENDING = 0,     // 待录取
  ADMITTED = 1,    // 已录取
  REJECTED = 2,    // 未录取
  WAITLISTED = 3   // 候补
}

// 批次枚举
export enum AdmissionBatch {
  FIRST = 1,       // 第一批
  SECOND = 2,      // 第二批
  THIRD = 3,       // 第三批
  SPECIAL = 4      // 特殊批次
}

@Entity('users')
export class User {
  @PrimaryColumn({ type: 'varchar', length: 20, comment: '用户ID，格式：TN_随机值12位' })
  id!: string;

  @Column({ type: 'varchar', length: 50, comment: '姓名' })
  name!: string;

  @Index('idx_id_card')
  @Column({ type: 'varchar', length: 18, name: 'id_card', comment: '身份证号' })
  idCard!: string;

  @Column({ 
    type: 'tinyint',
    default: Gender.MALE,
    comment: '性别：0 女 1 男'
  })
  gender!: Gender;

  @Index('idx_mobile')
  @Column({ type: 'varchar', length: 11, comment: '手机号' })
  mobile!: string;

  @Column({ type: 'varchar', length: 255, comment: '通讯地址' })
  address?: string;

  @Column({ type: 'varchar', length: 255, name: 'permanent_address', comment: '户籍地址' })
  permanentAddress?: string;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'id_card_front_photo', comment: '身份证正面照片URL' })
  idCardFrontPhoto?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'exam_number', comment: '考号' })
  examNumber?: string;

  @Column({ type: 'varchar', length: 20, nullable: true, comment: '省份' })
  province?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'exam_type', comment: '考试类型' })
  examType?: string;

  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: true, comment: '成绩' })
  score?: number;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'admission_ticket_photo', comment: '准考证照片URL' })
  admissionTicketPhoto?: string;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'first_choice', comment: '第一志愿' })
  firstChoice?: string;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'second_choice', comment: '第二志愿' })
  secondChoice?: string;

  @Column({ type: 'text', nullable: true, comment: '备注信息' })
  remarks?: string;

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '头像URL' })
  avatar?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, comment: '昵称' })
  nickname?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'parent_name', comment: '家长姓名' })
  parentName?: string;

  @Column({ type: 'varchar', length: 11, nullable: true, name: 'parent_mobile', comment: '家长手机号' })
  parentMobile?: string;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'admitted_major', comment: '录取专业' })
  admittedMajor?: string;

  @Column({ 
    type: 'tinyint',
    default: AdmissionResult.PENDING,
    name: 'admission_result',
    comment: '录取结果：0 待录取 1 已录取 2 未录取 3 候补'
  })
  admissionResult?: AdmissionResult;

  @Column({ 
    type: 'tinyint',
    nullable: true,
    name: 'admission_batch',
    comment: '批次：1 第一批 2 第二批 3 第三批 4 特殊批次'
  })
  admissionBatch?: AdmissionBatch;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'admission_notice_photo', comment: '录取通知图片URL' })
  admissionNoticePhoto?: string;

  @Column({ type: 'timestamp', nullable: true, name: 'admission_time', comment: '录取时间' })
  admissionTime?: Date;

  @Column({ 
    type: 'tinyint',
    default: PaymentStatus.UNPAID,
    name: 'payment_status',
    comment: '缴费状态：0 未缴费 1 已缴费 2 部分缴费'
  })
  paymentStatus?: PaymentStatus;

  @Index('idx_wx_unionid')
  @Column({ type: 'varchar', length: 100, nullable: true, name: 'wx_unionid', comment: '微信unionid' })
  wxUnionid?: string;

  @Index('idx_wx_openid')
  @Column({ type: 'varchar', length: 100, nullable: true, name: 'wx_openid', comment: '微信小程序openid' })
  wxOpenid?: string;

  @Column({ 
    type: 'tinyint',
    default: UserStatus.NORMAL,
    comment: '用户状态：0 正常 1 禁用'
  })
  status?: UserStatus;

  @Column({ 
    type: 'tinyint',
    default: SubscriptionStatus.UNSUBSCRIBED,
    name: 'subscription_status',
    comment: '消息订阅状态：0 未订阅 1 已订阅'
  })
  subscriptionStatus?: SubscriptionStatus;

  @CreateDateColumn({ 
    type: 'timestamp',
    name: 'create_time',
    comment: '创建时间'
  })
  createTime!: Date;

  @UpdateDateColumn({ 
    type: 'timestamp',
    name: 'update_time',
    comment: '修改时间'
  })
  updateTime!: Date;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'created_by', comment: '创建人' })
  createdBy?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'updated_by', comment: '修改人' })
  updatedBy?: string;

  @Column({ 
    type: 'tinyint',
    default: SystemUserStatus.NO,
    name: 'is_system',
    comment: '是否为系统用户：0 非系统用户 1 系统用户'
  })
  isSystem?: SystemUserStatus;

  constructor(data?: Partial<User>) {
    if (data) {
      Object.assign(this, data);
    }
  }

  get statusText(): string {
    const statusMap: { [key in UserStatus]: string } = {
      [UserStatus.NORMAL]: '正常',
      [UserStatus.DISABLED]: '禁用',
    };
    return statusMap[this.status ?? UserStatus.NORMAL];
  }

  get genderText(): string {
    const genderMap: { [key in Gender]: string } = {
      [Gender.MALE]: '男',
      [Gender.FEMALE]: '女'
    };
    return genderMap[this.gender ?? Gender.MALE];
  }

  get paymentStatusText(): string {
    const paymentStatusMap: { [key in PaymentStatus]: string } = {
      [PaymentStatus.UNPAID]: '未缴费',
      [PaymentStatus.PAID]: '已缴费',
      [PaymentStatus.PARTIAL]: '部分缴费'
    };
    return paymentStatusMap[this.paymentStatus ?? PaymentStatus.UNPAID];
  }

  get admissionResultText(): string {
    const admissionResultMap: { [key in AdmissionResult]: string } = {
      [AdmissionResult.PENDING]: '待录取',
      [AdmissionResult.ADMITTED]: '已录取',
      [AdmissionResult.REJECTED]: '未录取',
      [AdmissionResult.WAITLISTED]: '候补'
    };
    return admissionResultMap[this.admissionResult ?? AdmissionResult.PENDING];
  }

  get admissionBatchText(): string {
    const admissionBatchMap: { [key in AdmissionBatch]: string } = {
      [AdmissionBatch.FIRST]: '第一批',
      [AdmissionBatch.SECOND]: '第二批',
      [AdmissionBatch.THIRD]: '第三批',
      [AdmissionBatch.SPECIAL]: '特殊批次'
    };
    return admissionBatchMap[this.admissionBatch ?? AdmissionBatch.FIRST];
  }

  get isSystemText(): string {
    const isSystemMap: { [key in SystemUserStatus]: string } = {
      [SystemUserStatus.NO]: '普通用户',
      [SystemUserStatus.YES]: '系统用户'
    };
    return isSystemMap[this.isSystem ?? SystemUserStatus.NO];
  }
} 